package com.applcn.user.repo.repository;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.applcn.user.constant.AuthRangeEnum;
import com.applcn.user.dto.merchant.PageRoleReqDTO;
import com.applcn.user.repo.entity.RoleEntity;
import com.applcn.user.repo.entity.UserRoleEntity;
import com.applcn.user.repo.mapper.RoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * com.applcn.user.repo.repository
 * Description:
 *
 * @author: 大妖怪
 * @date: 2020-03-09 00:15
 */
@Repository
@RequiredArgsConstructor
public class RoleRepository extends ServiceImpl<RoleMapper, RoleEntity> {

    private final UserRoleRepository userRoleRepository;

    /**
     * 根据用户id和权限控制范围获取角色
     *
     * @param shopId
     * @param userId
     * @param authRange
     * @return
     */
    public List<RoleEntity> getRoleByUserIdAndAuthRange(Long shopId, Long userId, AuthRangeEnum authRange) {
        // TODO 此处的参数后根据业务扩大程度考虑封装
        // 获取用户角色
        List<UserRoleEntity> userRoleEntityList = userRoleRepository.getUserRoleListByUserId(userId);

        // 筛选角色列表
        List<Long> roleIdList = userRoleEntityList.stream().map(UserRoleEntity::getRoleId).collect(Collectors.toList());
        List<RoleEntity> roleEntityList = baseMapper.selectList(
                new QueryWrapper<RoleEntity>()
                        .lambda()
                        .eq(RoleEntity::getShopId, shopId)
                        .in(RoleEntity::getId, roleIdList)
                        .eq(RoleEntity::getAuthRange, authRange)
        );

        return roleEntityList;
    }

    /**
     * 分页查询角色列表
     *
     * @param dto
     * @return
     */
    public IPage<RoleEntity> pageRole(PageRoleReqDTO dto) {
        AuthRangeEnum authRange = AuthRangeEnum.getAuthRange(dto.getAuthRange());
        Assert.notNull(authRange, "范围不正确！");
        Page<RoleEntity> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<RoleEntity>().lambda();
        if (StrUtil.isNotBlank(dto.getRoleName())) {
            queryWrapper.like(RoleEntity::getName, dto.getRoleName());
        }
        queryWrapper.eq(RoleEntity::getShopId, dto.getShopId());
        queryWrapper.eq(RoleEntity::getAuthRange, authRange);

        return baseMapper.selectPage(
                page,
                queryWrapper
        );
    }

    /**
     * 批量删除角色
     *
     * @param ids
     * @param shopId
     */
    public void deleteBatch(List<Long> ids, Long shopId) {
        baseMapper.delete(
                new QueryWrapper<RoleEntity>()
                        .lambda()
                        .in(RoleEntity::getId, ids)
                        .eq(RoleEntity::getShopId, shopId)
        );
    }

    /**
     * 查询店铺角色列表
     *
     * @param authRangeEnum
     * @param shopId
     * @return
     */
    public List<RoleEntity> getShopRoleList(AuthRangeEnum authRangeEnum, Long shopId, List<Long> roleIds) {
        LambdaQueryWrapper<RoleEntity> wrapper = new QueryWrapper<RoleEntity>().lambda();
        wrapper.eq(RoleEntity::getAuthRange, authRangeEnum);
        wrapper.eq(RoleEntity::getShopId, shopId);
        if (CollectionUtil.isNotEmpty(roleIds)) {
            wrapper.in(RoleEntity::getId, roleIds);
        }
        return baseMapper.selectList(
                wrapper
        );
    }
}
